/**
 ******************************
 ****** 构造请求数据的函数 ******
 *****************************
 */

export function createQuestionData(data, imageUrl) {
    return {
        course_id: data.course_id,
        content: JSON.stringify([data.content.text, ...imageUrl]),
        title: data.title,
        chapter_lesson_id: data.chapter_id, //TODO 修改为lesson_id
        anonymity: data.anonymity
    };
}

export function modifyQuestionData(question, title, content, imageUrl) {
    return {
        question_id: question.question_id,
        course_id: question.course_id,
        content: JSON.stringify([content, ...imageUrl]),
        title,
        chapter_lesson_id: question.chapter_lesson_id, //TODO 修改为lesson_id
        anonymity: question.anonymity
    };
}


/**
 ******************************
 ******** 处理数据的函数 ********
 *****************************
 */
export function parseSingleQu(qu) {
    // console.log(qu);
    let images = null;
    //首先删除html标签
    qu.content = qu.content.replace(/<.*?>/g, '');
    try {
        //然后解析出数据，可能有images
        images = JSON.parse(qu.content);
        qu.content = images.shift();
    } catch (e) {
        //TODO handle the exception
        images = [];
    }
    qu.images = images; //把images放入字段images
    return qu;
}

export function parseBackendQuList(questions) {
    return questions.reduce((pre, value) => {
        parseSingleQu(value);
        pre.unshift(value); //倒序排列
        return pre;
    }, []);
}

